package com.dhwl.hsh.merchant.view;

import android.view.MotionEvent;
import android.widget.ListView;
import android.widget.ScrollView;

import com.dhwl.hsh.lib.utils.logger.Logger;

import java.util.logging.LogManager;

/**
 * 简要描述
 *
 * @author Derek.Kidd
 * @version V1.0.0
 * @date 2016-04-25 19:43
 */
public class ScrollViewWithListView extends ListView {


    private int maxHeight;

    private ScrollView parentScrollView;

    public ScrollViewWithListView(android.content.Context context,
                                  android.util.AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * Integer.MAX_VALUE >> 2,如果不设置，系统默认设置是显示两条
     */
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                setParentScrollAble(false);//当手指触到listview的时候，让父ScrollView交出ontouch权限，也就是让父scrollview  停住不能滚动

            case MotionEvent.ACTION_MOVE:

                Logger.d("onInterceptTouchEvent move");
                break;

            case MotionEvent.ACTION_UP:

                Logger.d("onInterceptTouchEvent up");
            case MotionEvent.ACTION_CANCEL:

                Logger.d("onInterceptTouchEvent cancel");

                setParentScrollAble(true);//当手指松开时，让父ScrollView重新拿到onTouch权限

                break;
            default:
                break;

        }
        return super.onInterceptTouchEvent(ev);

    }


    /**
     * 是否把滚动事件交给父scrollview
     *
     * @param flag
     */
    private void setParentScrollAble(boolean flag) {
        parentScrollView.requestDisallowInterceptTouchEvent(!flag);//这里的parentScrollView就是listview外面的那个scrollview
    }

    public int getMaxHeight() {
        return maxHeight;
    }

    public void setMaxHeight(int maxHeight) {
        this.maxHeight = maxHeight;
    }

    public ScrollView getParentScrollView() {
        return parentScrollView;
    }

    public void setParentScrollView(ScrollView parentScrollView) {
        this.parentScrollView = parentScrollView;
    }
}